package com.thinking.dubbo.redis.user;

import com.thinking.dubbo.redis.CommonRedisConnectionFactory;
import io.lettuce.core.ClientOptions;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
import io.lettuce.core.resource.DnsResolvers;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class UserRedisConfig {


    @Value("${redis.common.lettuce.pool.min-idle}")
    private Integer minIdle;
    @Value("${redis.common.lettuce.pool.max-idle}")
    private Integer maxIdle;
    @Value("${redis.common.lettuce.pool.max-active}")
    private Integer maxTotal;

    @Value("${business.redis.user.host}")
    private  String userHost;
    @Value("${business.redis.user.database}")
    private  Integer userDatabase;
    @Value("${business.redis.user.port}")
    private  Integer userPort;
    @Value("${business.redis.user.password}")
    private  String  userPassword;
    @Bean
    @ConditionalOnMissingBean
    CommonRedisConnectionFactory commonRedisConnectionFactory() {
        GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
        config.setMinIdle(minIdle);
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);

        Map<String, Object> source = new HashMap<>();
        source.put("spring.redis.host", userHost);
        source.put("spring.redis.port", userPort);
        source.put("spring.redis.database", userDatabase);
        source.put("spring.redis.password", userPassword);
         MapPropertySource property = new MapPropertySource("commonRedisClusterConfiguration", source);
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(property);

        ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
                .enablePeriodicRefresh(Duration.ofSeconds(30))
                .enableAllAdaptiveRefreshTriggers()
                .build();
        ClientOptions clientOptions = ClusterClientOptions.builder()
                .topologyRefreshOptions(topologyRefreshOptions)
                .build();
        ClientResources clientResources = DefaultClientResources.builder().dnsResolver(DnsResolvers.JVM_DEFAULT).build();
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().clientResources(clientResources)
                .clientOptions(clientOptions).poolConfig(config).build();

        return new CommonRedisConnectionFactory(clusterConfiguration, clientConfiguration);
    }

    @Bean
    UserRedisTemplate userRedisTemplate(CommonRedisConnectionFactory commonRedisConnectionFactory) {
        return new UserRedisTemplate(commonRedisConnectionFactory);
    }

}
